package com.jp.knowledge.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jp.knowledge.R;

/**
 * Created by 528787067@qq.com on 2017/6/26.
 */

public class InfoDetailSharePopupWindow extends PopupWindow implements View.OnClickListener {

	private Context mContext;
	private View contentView;
	private TextView shareEmail;
	private TextView shareTeamCircle;
	private TextView shareOther;
	private View line1;
	private View line2;

	public InfoDetailSharePopupWindow(Context context){
		mContext = context;
		contentView = View.inflate(mContext, R.layout.info_share_popupwindow, null);
		shareEmail = (TextView) contentView.findViewById(R.id.info_share_email);
		shareTeamCircle = (TextView) contentView.findViewById(R.id.info_share_team_circle);
		shareOther = (TextView) contentView.findViewById(R.id.info_share_other);
		line1 = contentView.findViewById(R.id.line_1);
		line2 = contentView.findViewById(R.id.line_2);
		shareEmail.setOnClickListener(this);
		shareTeamCircle.setOnClickListener(this);
		shareOther.setOnClickListener(this);

//		contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
		setContentView(contentView);
		setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
		setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		setOutsideTouchable(true);
		setFocusable(true);
		setBackgroundDrawable(new ColorDrawable(0x00000000));

		contentView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
			@Override
			public boolean onPreDraw() {
				contentView.getViewTreeObserver().removeOnPreDrawListener(this);
				dismiss();
				View parentView = (View) contentView.getTag();
				showAsDropDown(parentView,
						- contentView.getMeasuredWidth() + parentView.getMeasuredWidth(),
						- contentView.getMeasuredHeight() - parentView.getMeasuredHeight());
				return false;
			}
		});
	}

	@Override
	public void onClick(View v) {
		dismiss();
		if(onShareItemClickListener == null)
			return;
		switch (v.getId()){
			case R.id.info_share_email:
				onShareItemClickListener.onShareEmailClick();
				break;
			case R.id.info_share_team_circle:
				onShareItemClickListener.onShareTeamCircleClick();
				break;
			case R.id.info_share_other:
				onShareItemClickListener.onShareOtherClick();
				break;
		}
	}

	public void visibilityDownload(boolean visibility){
		shareEmail.setVisibility(visibility ? View.VISIBLE : View.GONE);
		line2.setVisibility(visibility ? View.VISIBLE : View.GONE);
	}

	public void visibilityTeamCircle(boolean visibility){
		shareTeamCircle.setVisibility(visibility ? View.VISIBLE : View.GONE);
		line1.setVisibility(visibility ? View.VISIBLE : View.GONE);
	}

	public void show(View parentView){
		contentView.setTag(parentView);
		contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
		showAsDropDown(parentView,
				- contentView.getMeasuredWidth() + parentView.getMeasuredWidth(),
				- contentView.getMeasuredHeight() - parentView.getMeasuredHeight());
	}

	private OnShareItemClickListener onShareItemClickListener;
	public void setOnShareItemClickListener(OnShareItemClickListener onShareItemClickListener){
		this.onShareItemClickListener = onShareItemClickListener;
	}
	public interface OnShareItemClickListener{
		void onShareEmailClick();
		void onShareTeamCircleClick();
		void onShareOtherClick();
	}
}
